package GUI;


import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import java.awt.Font;
import javax.swing.JButton;

import client.ClientConsole;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

/**
 * return book gui
 * @author Maor
 *
 */
public class GUI_returnBook extends JPanel {

	private ClientConsole clc;
	private JComboBox copiesBox;
	private JPanel backPanel;
	private GUI_LogIn gl;
	/**
	 * constructor that called from librerian menu, when the librerian want to return book
	 * @param cc  clientConsole
	 * @param gl  the main frame window
	 * @param lm  libreian menu, for back
	 */
	public GUI_returnBook(ClientConsole cc, final GUI_LogIn gl,final GUI_LibrarianMenu lm)
	{
		setLayout(null);
		this.setSize(700, 600);
		clc=cc;
		this.gl=gl;
		backPanel=lm;
		same();
	}
	/**
	 * 
	* constructor that called from Manager menu, when the Manager want to add  new reader card
	 * @param cc  clientConsole
	 * @param gl  the main frame window
	 * @param mm  Manager menu, for back
	 */
	public GUI_returnBook(ClientConsole cc, final GUI_LogIn gl,final GUI_ManagerMenu mm)
	{
		setLayout(null);
		this.setSize(700, 600);
		clc=cc;
		this.gl=gl;
		backPanel=mm;
		same();
	}
	
	/**
	 * same things in window for manager and librerian both
	 * when the user fill all the fields and press 'submit'  the method
	 * call to server for return the copy
	 */
	public void same()
	{
		JLabel lblCopiesInLoan = new JLabel("Select copy to return: ");
		lblCopiesInLoan.setBounds(10, 119, 190, 14);
		add(lblCopiesInLoan);
		
		copiesBox = new JComboBox();
		copiesBox.setBounds(222, 116, 95, 20);
		add(copiesBox);

		
		
		JLabel lblReturnBook = new JLabel("Return Book");
		lblReturnBook.setFont(new Font("Tahoma", Font.PLAIN, 17));
		lblReturnBook.setBounds(147, 52, 111, 14);
		add(lblReturnBook);
		
		JButton btnBack = new JButton("Back");
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				gl.setContentPane(backPanel);
			}
		});
		btnBack.setBounds(78, 201, 89, 23);
		add(btnBack);
		
		
		
		JButton btnLogOut = new JButton("Log out");
		btnLogOut.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				gl.LogOut();
			}
		});
		btnLogOut.setBounds(228, 201, 89, 23);
		add(btnLogOut);
		
		JButton btnSubmit = new JButton("Submit");
		btnSubmit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String copy=copiesBox.getSelectedItem().toString();
				String[] s={"return copy",copy};
					try {
						clc.client.sendToServer(s);
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
			
				while(clc.client.flag==0)
					System.out.print("");
				clc.client.flag=0;
				JOptionPane.showMessageDialog(new JFrame(),clc.client.message);
				init();
			}
		});
		btnSubmit.setBounds(345, 115, 89, 23);
		add(btnSubmit);
		init();
	}
	
	public void init()
	{
		
		copiesBox.removeAllItems();
		String[] s={"get copies in loan"};
		try {
			clc.client.sendToServer(s);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		while(clc.client.flag==0)
			System.out.print("");
		clc.client.flag=0;
		ArrayList<String[]> result;
		result=clc.client.details;
		Iterator i=result.iterator();
		while(i.hasNext())
		{
		String[] res=(String[]) i.next();
		copiesBox.addItem(res[0]);
		}
		
	}
}


